package com.tfjybj.itoo.teach.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.InstitutionEntity;
import com.tfjybj.itoo.teach.model.InstitutionModel;
import com.tfjybj.itoo.teach.provider.service.InstitutionService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * InstitutionController
 * institution表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-08 19:34:10
 */
@Api(tags = {"institution表接口"})
@RequestMapping(value = "/institution")
@RestController
public class InstitutionController {

    @Resource
    private InstitutionService institutionService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model InstitutionModel
     * @return 添加的结果（修改，加入判重，设置默认type）
     * @author 杨燕梅
     * @since 2.0.0 2018-12-20 14:40:59
     */
    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping(value = {"/create"})
    @Transactional(rollbackFor = Exception.class)
    public ItooResult create(@RequestBody InstitutionModel model) {
		if (StringUtils.isEmpty(model.getName())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
		if (StringUtils.isEmpty(model.getCode())){
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        //判断党政部门是否重复
        if (!CollectionUtils.isEmpty(institutionService.queryByCodeAndNameExist(model.getCode(),model.getName()))){
            return ItooResult.build(ItooResult.FAIL, "教学部门已存在");
        }
        InstitutionEntity institutionEntity = new InstitutionEntity();
        BeanUtils.copyProperties(model, institutionEntity);
        institutionEntity.setType(0);
        institutionService.save(institutionEntity);
        institutionService.saveToAuth(institutionEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功          
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id删除（单个）", notes = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        institutionService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id批量删除", notes = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        institutionService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model InstitutionModel
     * @return 修改后的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id修改institution", notes = "根据id修改institution")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody InstitutionModel model) {
		if (StringUtils.isEmpty(model.getName())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
		if (StringUtils.isEmpty(model.getCode())){
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        InstitutionEntity institutionEntity = new InstitutionEntity();
        BeanUtils.copyProperties(model, institutionEntity);
        institutionService.updateById(institutionEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找Institution
     *
     * @param id 主键id
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        InstitutionEntity institutionEntity = institutionService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", institutionEntity);
    }

    /**
     * 分页查询所有Institution
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "分页查询所有Institution", notes = "分页查询所有Institution")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<InstitutionEntity> institutions = institutionService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", institutions);
    }
	
	/**
     * 根据机构名称模糊查询institution
     *
     * @param name  机构名称
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的institution
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据机构名称模糊查询institution", notes = "分页根据机构名称模糊查询institution")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", required = true, example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录数", dataType = "int", required = true, example = "10")
    })
    @GetMapping(value = "queryInstitutionByLikeName/{pageNo}/{pageSize}")
    public ItooResult queryInstitutionByLikeName(@RequestParam(required = false, defaultValue = "") String name,
                                                 @PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        PageInfo<InstitutionEntity> institutionList = institutionService.queryByLikeName(name, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", institutionList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */
    /**
     * 查询所有党政部门
     * @return Institution
     * @author 杨燕梅
     * @since 2.0.0 2018-11-23 15:26:15
     */
    @ApiOperation(value = "查询所有党政部门",notes = "查询所有党政部门")
    @GetMapping(value = {"/queryAllInstitution"})
    public ItooResult queryAllInstitution() {
        List<InstitutionModel> institutionList = institutionService.queryAllInstitution();
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", institutionList);
    }

    /**
     * 查询所有党政部门（带分页）
     * @author 杨燕梅
     * @since 2.0.0 2018-12-19 16:12:30
     */
    @ApiOperation(value = "查询所有党政部门（带分页）", notes = "查询所有党政部门（带分页）")
    @GetMapping(value = "queryAllInstitutionPageInfo/{pageNo}/{pageSize}")
    public ItooResult queryAllInstitutionPageInfo(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1") @PathVariable Integer pageNo,
                                                  @ApiParam(name = "pageSize",value = "页数",required = true,example = "10") @PathVariable Integer pageSize) {
        PageInfo<InstitutionModel> academyList = institutionService.queryAllInstitutionPageInfo(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", academyList);
    }

    /**
     * 根据名称查ID（学院和机构）-张家豪
     * @return Institution
     * @author 杨燕梅
     * @since 2.0.0 2018-11-25 16:00:30
     */
    @ApiOperation(value = "根据名称查ID（学院和机构）",notes = "根据名称查ID（学院和机构）")
    @GetMapping(value = {"/queryAcademyAndInstitutionIdByName/{id}"})
    public ItooResult queryAcademyAndInstitutionIdByName(@PathVariable String id) {
        List<InstitutionModel> institutionList = institutionService.queryAcademyAndInstitutionIdByName(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", institutionList);
    }

    /**
     * 根据党政部门名称模糊查询党政部门（带分页）
     *
     * @param searchContent  输入的党政部门名称
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的党政部门
     * @author 杨燕梅
     * @since 2.0.0 2018-12-7 14:57:07
     */
    @ApiOperation(value = "根据党政部门名称模糊查询党政部门（带分页）", notes = "根据党政部门名称模糊查询党政部门（带分页）")
    @GetMapping(value = "queryInstitutionByLikeInstitutionName/{pageNo}/{pageSize}")
    public ItooResult queryInstitutionByLikeInstitutionName(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1") @PathVariable Integer pageNo,
                                                            @ApiParam(name = "pageSize",value = "页数",required = true,example = "10") @PathVariable Integer pageSize,
                                                            @RequestParam(required = false, defaultValue = "") String searchContent) {
        PageInfo<InstitutionModel> institutionList = institutionService.queryInstitutionByLikeName(searchContent, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", institutionList);
    }

    /**
     * 根据党政部门ID查询党政部门详情
     *
     * @param idList 党政部门ID
     * @return 党政部门实体集合
     * @author 杨燕梅
     * @since 2.0.0 2018-12-12 17:43:28
     */
    @ApiOperation(value = "根据党政部门ID查询党政部门详情", notes = "根据党政部门ID查询党政部门详情")
    @GetMapping(value = "/queryInstitutionById/{idList}")
    public ItooResult queryInstitutionById(@ApiParam(name = "idList", value = "党政部门ID", required = true) @PathVariable List<String> idList) {
        List<InstitutionModel> institutionModelList =institutionService.queryInstitutionById(idList);
        return ItooResult.build(ItooResult.SUCCESS, "查询党政部门成功!", institutionModelList);
    }

    /**
     * 根据ID导出党政部门信息
     *
     * @param response 响应
     * @param idList   党政部门ID
     * @return 导出结果信息
     * @author 杨燕梅
     * @since 2.0.0 2018-12-12 17:44:38
     */
    @ApiOperation(value = "根据ID导出党政部门信息", notes = "根据ID导出党政部门信息")
    @GetMapping(value = {"/exportAll"})
    public ItooResult exportAll(HttpServletResponse response, @RequestParam(required = false, defaultValue = "") List<String> idList) {
        return institutionService.exportByInstitutionId(response, idList);
    }

    /**
     * 根据条件导出党政部门
     *
     * @param response     响应
     * @param searchContent    党政部门名称
     * @return 导出结果信息
     * @author 杨燕梅
     * @since 2.0.0 2018-12-23 15:57:02
     */
    @ApiOperation(value = "根据条件导出党政部门", notes = "根据条件导出党政部门")
    @GetMapping(value = {"/exportByStrLike"})
    public ItooResult exportByStrLike(HttpServletResponse response, @RequestParam(required = false, defaultValue = "") String searchContent) {
        return institutionService.exportByStrLike(response,searchContent);
    }

    /**
     * 下载党政部门模板，批量导入使用
     *
     * @param response 响应
     * @return 下载模板是否成功
     * @author 杨燕梅
     * @since 2.0.0 2018-11-30 10:07:31
     */
    @ApiOperation(value = "下载党政部门模板，批量导入使用", notes = "下载党政部门模板，批量导入使用")
    @GetMapping(value = {"/downLoadInstitutionTemplate"})
    public ItooResult downLoadInstitutionTemplate(HttpServletResponse response) {
        if (institutionService.downLoadTemplate(response)) {
            return ItooResult.build(ItooResult.SUCCESS, "下载党政部门模板成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "下载党政部门模板失败");
        }
    }

    /**
     * 批量导入党政部门
     *
     * @param multipartFile 批量导入的文件
     * @param request       请求
     * @param response      响应
     * @return ItooResult 批量导入的结果
     * @author 杨燕梅
     * @since 2.0.0 2018-11-30 11:59:42
     */
    @ApiOperation(value = "批量导入党政部门", notes = "0001:部分成功；0000:成功；1111:失败")
    @PostMapping(value = {"/importInstitutionInfoByTemplate"})
    public ItooResult importInstitutionInfoByTemplate(@RequestParam(value = "file") MultipartFile multipartFile,
                                                      HttpServletRequest request, HttpServletResponse response) {
        return institutionService.importTemplate(multipartFile, request, response);
    }

    /**
     * 根据errorListId导出未成功导入的党政部门信息
     *
     * @param response    响应
     * @param errorListId 为成功导入的分组id
     * @return 导出结果
     * @author 杨燕梅
     * @since 1.0.0 2018-12-1 23:27:30
     */
    @ApiOperation(value = "根据errorListId导出未成功导入的党政部门信息", notes = "根据errorListId导出未成功导入的党政部门信息")
    @GetMapping(value = {"/exportErrorInstitutionById/{errorListId}"})
    public ItooResult exportErrorInstitutionById(HttpServletResponse response,
                                                 @ApiParam(name = "errorListId", value = "errorListId", required = true) @PathVariable String errorListId) {
        if (institutionService.exportErrorList(errorListId, response)) {
            return ItooResult.build(ItooResult.SUCCESS, "导出成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "导出失败");
        }
    }

    /**
     * 根据党政部门代码和党政部门名称查询党政部门是否已存在
     *
     * @param code   党政部门代码
     * @param name   党政部门名称
     * @return 党政部门信息
     * @author 杨燕梅
     * @since 2.0.0 2018-12-20 09:05:59
     */
    @ApiOperation(value = "根据党政部门代码和党政部门名称查询党政部门是否已存在", notes = "根据党政部门代码和党政部门名称查询党政部门是否已存在")
    @GetMapping(value = {"/queryByCodeAndNameExist/{code}/{name}"})
    public ItooResult queryByCodeAndNameExist(@ApiParam(name = "code", value = "党政部门代码", required = true) @PathVariable String code,
                                              @ApiParam(name = "name", value = "党政部门名称", required = true) @PathVariable String name) {
        List<InstitutionEntity> institutionList = institutionService.queryByCodeAndNameExist(code,name);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", institutionList);
    }

}    
